CalendarGcNumberCellTypeの基本的な使い方について説明します。
書式の設定
CalendarGcNumberCellType.Fieldsプロパティを使用します。
フィールド
CalendarGcNumberCellTypeでは書式設定により、数値の入力および表示書式を自由に設定することができます。入力書式は、セルが入力フォーカスを受け取ったときの書式で、表示書式は入力フォーカスのないときの書式です。
入力書式の設定は、Fieldsプロパティによって行われ、フィールドとよばれる入力領域を構成する要素によって定義されます。また、表示書式は、DisplayFieldsプロパティによって行われ、Fieldsプロパティと同様フィールドによって定義します。FieldsプロパティおよびDisplayFieldsプロパティについては、「書式を設定する」で詳しく解説します。
CalendarGridのデザイナ上で書式を設定するには、入力書式・表示書式共に、次の2通りのデザイン機能が用意されています。
- フィールドコレクションエディタを使用する
- キーワードから書式を生成する
フィールドコレクションエディタを使用する
CalendarGcNumberCellTypeで設定可能なフィールドを組み合わせて書式を設定する方法です。次のいずれかの方法で設定することができます。
入力書式
- プロパティウィンドウのFieldsプロパティから開かれる[フィールドコレクションエディタ]
表示書式
- プロパティウィンドウのDisplayFieldsプロパティから開かれる[フィールドコレクションエディタ]
これらのデザイン機能では、コレクションにフィールドを追加すると同時に各フィールドのテキストのフォントや色といったスタイルを設定することができます。


(図)フィールド毎に異なったスタイルを設定
キーワードから書式を生成する
キーワードを設定することで書式を設定します。個々のフィールドのプロパティは設定できませんが、一般的な数値のフォーマットが予めビルトイン書式として用意されているため、これらのリストから書式を選択するだけで簡単に書式を定義することが可能です。
キーワードから書式を設定するには、キーワードから書式を設定するには、CalendarGridデザイナでCalendarGcNumberCellTypeが設定されているセルを選択してプロパティウィンドウのCellType.Fieldsプロパティから「入力フィールドの編集」ダイアログを開き、ツールボタンの右端の「書式を指定してフィールドを設定」をクリックして、ドロップダウンリストからキーワードを選択します。表示書式の場合はCellType.DisplayFieldsプロパティを使用します。
キーワードを使って設定した書式は、フィールドオブジェクトのコレクションに変換されます。フィールドオブジェクトに変換された後はフィールドエディタで編集できるようになります。
値の端数処理
CalendarGcNumberCellType.Fieldsプロパティを使用します。
CalendarGcNumberCellTypeに設定した書式に対して、端数処理の詳細な設定ができます。 端数処理は既定で四捨五入が設定されますが、それ以外の端数処理を行いたい場合にはRoundPatternプロパティを変更します。
プロパティ値 |
説明 |
Ceiling |
切り上げ(大きいほうの近似値への丸め) |
Floor |
切り捨て(小さいほうの近似値への丸め) |
MidpointRoundAwayFromZero |
四捨五入 |
MidpointRoundToEven |
近似値への丸め(偶数丸め、銀行丸め) |
マイナス値の設定と色
CalendarGcNumberCellType.ValueSignプロパティおよび CalendarGcNumberCellType.UseNegativeColorプロパティを使用します。
セルの入力をプラスの値かマイナスの値のいずれかのみを許可したい場合、ValueSignプロパティを設定します。
プロパティ値 |
説明 |
NoControl |
数値の符号が設定されていません。正数、負数両方の入力を許可します。 |
Positive |
正数の入力のみを許可します。 |
Negative |
負数の入力のみを許可します。 |
マイナス値が入力されたとき、書式の設定「書式を設定する」で表示する文字列を指定することが可能ですが、NegativeColorプロパティを使用すれば、マイナス値が入力された場合の文字色を設定することができます。また、UseNegativeColorプロパティで、指定した色を適用するかどうかを設定します。

(図) マイナス値の書式と色を設定したCalendarGcNumberCellType
マイナスキーの動作
CalendarGcNumberCellType.ShortcutKeysプロパティを使用します。
マイナスキーは値をプラスとマイナスを切り替えるスイッチ機能となっていました。(1回目のマイナスキー押下で値がマイナスに変化、2回目押下でプラスに変化) デフォルトではスイッチ機能のまま動作します。
このデフォルトの動作は、CalendarGcNumberCellTypeに以下のショートカット機能が設定されていることで実現されています。
- キー:OemMinusに対してショートカット動作のSwitchSign
- キー:Subtractに対してショートカット動作のSwitchSign
この動作を、マイナスキーが押下された場合には値をマイナスに変更するだけの動作とさせることもできます。(1回目のマイナスキー押下で値がマイナスに変化、2回目押下でもマイナスのまま。プラスにしたい場合はプラスキーを押下する)
デフォルトの動作から新しい動作に変更するためには、上記で挙げたふたつのショートカット機能を削除します。
リテラル文字列を含まない値の設定と取得
CalendarGcNumberCellType.Valueプロパティを使用します。
Valueプロパティを使えば、リテラル文字列を除いたセル内の数値をDecimal型で取得または設定できます。
クリップボードにリテラル文字を含まない値を渡すには、ClipContentプロパティとGcNumberEditingControl.SelectedTextプロパティを使用します。ClipContentプロパティで制御できるのは、SelectedTextプロパティの値のみなので、以下のプロパティの値をクリップボードに設定した場合は、ClipContentプロパティの設定は無効になります。
次のサンプルコードは、ClipContentプロパティを使って、リテラル文字列を省いた文字列をクリップボードにコピーする方法を示します。
Imports GrapeCity.Win.CalendarGrid
Imports InputManCell = GrapeCity.Win.CalendarGrid.InputMan
Imports CalendarGridInputMan = GrapeCity.Win.CalendarGrid.Editors
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim today As DateTime = DateTime.Today
Dim GcNumberCellType As New InputManCell.CalendarGcNumberCellType()
' セルを設定します。
GcNumberCellType.Fields.SignPrefix.PositivePattern = "\"
GcNumberCellType.Fields.SignPrefix.NegativePattern = "\-"
GcNumberCellType.ClipContent = InputMan.ClipContent.ExcludeLiterals
Dim TextBoxCellType As New CalendarTextBoxCellType()
GcCalendarGrid1.Content(today).Rows(1).Cells(0).CellType = GcNumberCellType
GcCalendarGrid1.Content(today).Rows(1).Cells(0).Value = 10000
GcCalendarGrid1.Content(today).Rows(2).Cells(0).CellType = TextBoxCellType
GcCalendarGrid1.ScrollIntoView(today)
End Sub
Private Sub GcCalendarGrid1_EditingControlShowing(sender As Object, e As CalendarEditingControlShowingEventArgs) Handles GcCalendarGrid1.EditingControlShowing
If TypeOf e.Control Is CalendarGridInputMan.GcNumber Then
' GcNumberCellの編集用コントロールが表示された場合にKeyDownイベントを設定します。
RemoveHandler e.Control.KeyDown, AddressOf Editor_KeyDown
AddHandler e.Control.KeyDown, AddressOf Editor_KeyDown
End If
End Sub
Private Sub Editor_KeyDown(sender As Object, e As KeyEventArgs)
Dim cell As CalendarCell = GcCalendarGrid1(GcCalendarGrid1.CurrentCellPosition.Date) _
(GcCalendarGrid1.CurrentCellPosition.RowIndex, GcCalendarGrid1.CurrentCellPosition.ColumnIndex)
Dim editor As CalendarGridInputMan.GcNumber = DirectCast(sender, CalendarGridInputMan.GcNumber)
If e.KeyCode = Keys.F5 Then
' クリップボードにコピーします。
editor.SelectionStart = 0
editor.SelectionLength = editor.Text.Length
Clipboard.SetDataObject(editor.SelectedText)
' クリップボードのデータを取得して確認します。
Dim cbData As IDataObject = Clipboard.GetDataObject()
GcCalendarGrid1(GcCalendarGrid1.CurrentCellPosition.Date)(cell.RowIndex + 1, cell.ColumnIndex).Value =
cbData.GetData(DataFormats.Text).ToString()
End If
End Sub
using GrapeCity.Win.CalendarGrid;
using InputManCell = GrapeCity.Win.CalendarGrid.InputMan;
using CalendarGridInputMan = GrapeCity.Win.CalendarGrid.Editors;
private void Form1_Load(object sender, EventArgs e)
{
var today = DateTime.Today;
var gcNumberCellType = new InputManCell.CalendarGcNumberCellType();
// セルを設定します。
gcNumberCellType.Fields.SignPrefix.PositivePattern = "\\";
gcNumberCellType.Fields.SignPrefix.NegativePattern = "\\-";
gcNumberCellType.ClipContent = InputManCell.ClipContent.ExcludeLiterals;
CalendarTextBoxCellType textBoxCellType = new CalendarTextBoxCellType();
gcCalendarGrid1.Content[today].Rows[1].Cells[0].CellType = gcNumberCellType;
gcCalendarGrid1.Content[today].Rows[1].Cells[0].Value = 10000;
gcCalendarGrid1.Content[today].Rows[2].Cells[0].CellType = textBoxCellType;
gcCalendarGrid1.ScrollIntoView(today);
gcCalendarGrid1.EditingControlShowing += gcCalendarGrid1_EditingControlShowing;
}
private void gcCalendarGrid1_EditingControlShowing(object sender, CalendarEditingControlShowingEventArgs e)
{
if (e.Control is CalendarGridInputMan.GcNumber)
{
// GcNumberCellの編集用コントロールが表示された場合にKeyDownイベントを設定します。
e.Control.KeyDown -= new KeyEventHandler(Editor_KeyDown);
e.Control.KeyDown += new KeyEventHandler(Editor_KeyDown);
}
}
private void Editor_KeyDown(object sender, KeyEventArgs e)
{
CalendarCell cell = gcCalendarGrid1[gcCalendarGrid1.CurrentCellPosition.Date]
[gcCalendarGrid1.CurrentCellPosition.RowIndex, gcCalendarGrid1.CurrentCellPosition.ColumnIndex];
var editor = sender as CalendarGridInputMan.GcNumber;
if (e.KeyCode == Keys.F5)
{
// クリップボードにコピーします。
editor.SelectionStart = 0;
editor.SelectionLength = editor.Text.Length;
Clipboard.SetDataObject(editor.SelectedText);
// クリップボードのデータを取得して確認します。
IDataObject cbData = Clipboard.GetDataObject();
gcCalendarGrid1[gcCalendarGrid1.CurrentCellPosition.Date][cell.RowIndex + 1, cell.ColumnIndex].Value =
cbData.GetData(DataFormats.Text).ToString();
}
}
ゼロを削除できるかどうかの設定
CalendarGcNumberCellType.AllowDeleteToNullプロパティを使用します。
CalendarGcNumberCellTypeで、ゼロを削除し値をNullとすることができるかどうかは、AllowDeleteToNullプロパティで設定します。
AllowDeleteToNullプロパティをTrueに設定することで、入力された「0」をDeleteキーやBackSpaceキーで削除し、そのValueプロパティをNullにすることができます。
編集モードの切り替え
CalendarGcNumberCellType.EditModeプロパティを使用します。
EditModeプロパティを使って、セルがフォーカスを受け取ったときのデフォルトの編集モードを定義できます。EditModeプロパティをEditMode.Insertにすると挿入モード、EditMode.Overwriteにすると上書きモードになります。また、EditMode.FixedInsertとEditMode.FixedOverwriteでは、編集モードが固定されるので、実行中に[Ins]キーが押されても編集モードは切り替わりません。
セルの編集時では、EditModeプロパティがEditMode.InsertまたはEditMode.Overwriteに設定されている場合、編集モードが[Ins]キーまたはEditModeプロパティによって変更されたときには、GcNumberEditingControl.EditStatusChangedイベントが発生します。また、フォーカス取得時の編集モードは、Overwriteプロパティを使って調べることができます。
次のサンプルコードは、GcNumberEditingControl.KeyDownイベントで[Alt]キーおよび[C]キーが押下されたとき、GcNumberEditingControl.EditModeプロパティを使用して編集モードを取得します。
Imports GrapeCity.Win.CalendarGrid
Imports InputManCell = GrapeCity.Win.CalendarGrid.InputMan
Imports CalendarGridInputMan = GrapeCity.Win.CalendarGrid.Editors
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim today As DateTime = DateTime.Today
Dim GcNumberCellType As New InputManCell.CalendarGcNumberCellType()
GcNumberCellType.EditMode = InputMan.EditMode.Insert
GcCalendarGrid1.Content(today).Rows(1).Cells(0).CellType = GcNumberCellType
GcCalendarGrid1.ScrollIntoView(today)
End Sub
Private Sub GcCalendarGrid1_EditingControlShowing(sender As Object, e As CalendarEditingControlShowingEventArgs) Handles GcCalendarGrid1.EditingControlShowing
If TypeOf e.Control Is CalendarGridInputMan.GcNumber Then
' GcNumberCellの編集用コントロールが表示された場合にKeyDownイベントを設定します。
RemoveHandler e.Control.KeyDown, AddressOf Editor_KeyDown
AddHandler e.Control.KeyDown, AddressOf Editor_KeyDown
End If
End Sub
Private Sub Editor_KeyDown(sender As Object, e As KeyEventArgs)
Dim editor As CalendarGridInputMan.GcNumber = DirectCast(sender, CalendarGridInputMan.GcNumber)
If e.Alt AndAlso e.KeyCode = Keys.C Then
If editor.OverWrite = True Then
Console.WriteLine("上書き")
Else
Console.WriteLine("挿入")
End If
End If
End Sub
using GrapeCity.Win.CalendarGrid;
using InputManCell = GrapeCity.Win.CalendarGrid.InputMan;
using CalendarGridInputMan = GrapeCity.Win.CalendarGrid.Editors;
private void Form1_Load(object sender, EventArgs e)
{
var today = DateTime.Today;
var gcNumberCellType = new InputManCell.CalendarGcNumberCellType();
gcNumberCellType.EditMode = InputManCell.EditMode.Insert;
gcCalendarGrid1.Content[today].Rows[1].Cells[0].CellType = gcNumberCellType;
gcCalendarGrid1.ScrollIntoView(today);
gcCalendarGrid1.EditingControlShowing += gcCalendarGrid1_EditingControlShowing;
}
private void gcCalendarGrid1_EditingControlShowing(object sender, CalendarEditingControlShowingEventArgs e)
{
if (e.Control is CalendarGridInputMan.GcNumber)
{
// GcNumberCellの編集用コントロールが表示された場合にKeyDownイベントを設定します。
e.Control.KeyDown -= new KeyEventHandler(Editor_KeyDown);
e.Control.KeyDown += new KeyEventHandler(Editor_KeyDown);
}
}
private void Editor_KeyDown(object sender, KeyEventArgs e)
{
var editor = sender as CalendarGridInputMan.GcNumber;
if (e.Alt && e.KeyCode == Keys.C)
{
if (editor.OverWrite == true)
{
Console.WriteLine("上書き");
}
else
{
Console.WriteLine("挿入");
}
}
}
イベントを使った文字列操作
GcNumberEditingControl.TextChangingイベントを使用します。
現在カレットが置かれているフィールドは、GcNumberEditingControl.ActiveFieldプロパティで取得できます。
セルに文字列を入力するとGcNumberEditingControl.TextChangingイベントが、GcNumberEditingControl.TextChangedイベントの前(入力された文字列がTextプロパティに渡される前)に発生します。そのため、このイベント内で入力文字列をチェックすれば、Textプロパティの値に影響を与えることなく、入力を制御できます。
コンテキストメニューの表示
CalendarGridの他のセル型の場合と同様です。CalendarGcNumberCellType.ContextMenuStripを使用します。
関連トピック